package com.juno.cafeNoa.admin.adapter;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.juno.cafeNoa.admin.R;
import com.juno.cafeNoa.admin.data.Member;
import com.juno.cafeNoa.admin.net.AsyncTaskCompleteListener;
import com.juno.cafeNoa.admin.net.HttpPostTask;
import com.juno.cafeNoa.admin.net.NetConstant;
import com.juno.cafeNoa.admin.util.Util;

public class MemberADP extends ArrayAdapter<Member> {
	private Context mContext;
	private int mResource;
	private ArrayList<Member> mList;
	private LayoutInflater mInflater;

	public MemberADP(Context context, int layoutResource, ArrayList<Member> list) {
		super(context, layoutResource, list);
		this.mContext = context;
		this.mResource = layoutResource;
		this.mList = list;
		this.mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if ( convertView == null ) {
			convertView = mInflater.inflate(R.layout.row_member, null);
			holder = new ViewHolder();
			holder.id = (TextView) convertView.findViewById(R.id.userId);
			holder.phone = (TextView) convertView.findViewById(R.id.userPhone);
			holder.point = (TextView) convertView.findViewById(R.id.userPoint);
			holder.grade = (Button) convertView.findViewById(R.id.userGrade);
			holder.black = (Button) convertView.findViewById(R.id.userBlack);
			
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.id.setText(mList.get(position).getId());
		holder.phone.setText(mList.get(position).getPhone());
		holder.point.setText(String.valueOf(mList.get(position).getPoint()));
		holder.grade.setText( (mList.get(position).isGrade()) ? "단골" : "일반");
		
		if(mList.get(position).isGrade())
			holder.id.setTextColor(Color.BLUE);
		
		holder.grade.setOnClickListener(new ClickListener(position));
		
		holder.black.setText( (mList.get(position).isBlack())? "블랙리스트" : "보통");
		holder.black.setOnClickListener(new ClickListener(position));
		
		holder.position = position;

		return convertView;
	}

	class ViewHolder {
		int position;
		
		TextView id;
		TextView phone;
		TextView point;
		Button grade;
		Button black;
	}
	
	int mPosition = -1;
	
	class ClickListener implements OnClickListener{
		int position;
		String value;
		String nextValue;
		String changeFlag;
		public ClickListener(int position){
			this.position = position;
		}
				
		@Override
		public void onClick(View v) {
			mPosition = position;
			if(v.getId()==R.id.userGrade){
				value = (mList.get(mPosition).isGrade()) ? "단골":"보통";
				nextValue = (mList.get(mPosition).isGrade()) ? "보통":"단골";
				changeFlag = "V";
				
				showAlert();
			} else if(v.getId()==R.id.userBlack){
				value = (mList.get(mPosition).isBlack()) ? "블랙리스트":"보통";
				nextValue = (mList.get(mPosition).isBlack()) ? "보통":"블랙리스트";
				changeFlag = "B";
				
				showAlert();
			}
			
		}
		
		public void showAlert(){
			
			AlertDialog.Builder ab = new AlertDialog.Builder(mContext);
			ab.setTitle("확인");
			ab.setIcon(R.drawable.ic_launcher);
			ab.setMessage("현재상태( "+ value +") 를\n("+nextValue+")로 바꾸시겠습니까 ?");
			ab.setPositiveButton("확인", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					if(changeFlag.equalsIgnoreCase("V"))
						chGrade.excuteHttpTask();
					else if(changeFlag.equalsIgnoreCase("B"))
						chBlack.excuteHttpTask();
				}
			});
			
			ab.setNegativeButton("취소", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					
				}
			});
			
			AlertDialog alert = ab.create();
			alert.show();
		}
		
	}
	AsyncTaskCompleteListener chGrade = new AsyncTaskCompleteListener() {

		@Override
		public void excuteHttpTask() {
			//Url ����.
			String url = Util.getUrl("ModifyMember");
			String chvalue = (mList.get(mPosition).isGrade()) ? "0" : "1";
			ArrayList<NameValuePair> value = new ArrayList<NameValuePair>();
			value.add(new BasicNameValuePair("USER_ID", mList.get(mPosition).getId()));
			value.add(new BasicNameValuePair("CHANGE_FLAG", "V"));
			
			HttpPostTask http = new HttpPostTask(mContext, value, url, chGrade);
			http.execute();
		}

		@Override
		public void onTaskComplete(String result, boolean taskOk) {
			Log.v("result","reuslt :" + result);
			if(taskOk){
				int code = -1;
				
				try {
					JSONObject object = new JSONObject(result);
					code = object.getInt("RESULT");

					if(code == NetConstant.NETWORK_SUCCESS){
						Toast.makeText(mContext, "정상처리되었습니다.", Toast.LENGTH_LONG).show();
					} else {
						Toast.makeText(mContext, "네트워크오류.!", Toast.LENGTH_LONG).show();
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		}
		
	};
	
	AsyncTaskCompleteListener chBlack = new AsyncTaskCompleteListener() {

		@Override
		public void excuteHttpTask() {
			String url = Util.getUrl("ModifyMember");
			String chvalue = (mList.get(mPosition).isBlack()) ? "0" : "1";
			ArrayList<NameValuePair> value = new ArrayList<NameValuePair>();
			value.add(new BasicNameValuePair("USER_ID", mList.get(mPosition).getId()));
			value.add(new BasicNameValuePair("CHANGE_FLAG", "B"));
			
			HttpPostTask http = new HttpPostTask(mContext, value, url, chBlack);
			http.execute();
		}

		@Override
		public void onTaskComplete(String result, boolean taskOk) {
			Log.v("result","reuslt :" + result);
			if(taskOk){
				int code = -1;
				
				try {
					JSONObject object = new JSONObject(result);
					code = object.getInt("RESULT");

					if(code == NetConstant.NETWORK_SUCCESS){
						Toast.makeText(mContext, "정상처리되었습니다.", Toast.LENGTH_LONG).show();
					} else {
						Toast.makeText(mContext, "네트워크 오류!", Toast.LENGTH_LONG).show();
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		}
		
	};
	


}
